
package common;

import enums.CustomerEnum;
import Swing.CustomerScreen;
import dao.CustomerDao;
import entities.Customer;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

public class CustomerUtils implements MessagesForUser{
    CustomerDao customerDao = new CustomerDao();
    private final String SUCCESS_SAVE = "Dane zostały zapisane pomyślnie";
    
    public ArrayList<Customer> getCustomerData(){
         return customerDao.getAllCustomers();
    }
    
    public void saveNewCustomer(HashMap<String, String> map){
        Customer newCustomer = new Customer();
        if(map.containsKey(CustomerEnum.firstName.name())){
            newCustomer.setFirstName(map.get(CustomerEnum.firstName.name()));
        }
        
        if(map.containsKey(CustomerEnum.lastName.name())){
            newCustomer.setLastName(map.get(CustomerEnum.lastName.name()));
        }
        
        if(map.containsKey(CustomerEnum.street.name())){
            newCustomer.setStreet(map.get(CustomerEnum.street.name()));
        }
        
        if(map.containsKey(CustomerEnum.postCode.name())){
            newCustomer.setPostCode(map.get(CustomerEnum.postCode.name()));
        }
        
        if(map.containsKey(CustomerEnum.city.name())){
            newCustomer.setCity(map.get(CustomerEnum.city.name()));
        }
        
        if(map.containsKey(CustomerEnum.phone.name())){
            newCustomer.setPhone(map.get(CustomerEnum.phone.name()));
        }
        
        if(map.containsKey(CustomerEnum.email.name())){
            newCustomer.setEmail(map.get(CustomerEnum.email.name()));
        }
        
        if(map.containsKey(CustomerEnum.nip.name())){
            newCustomer.setNip(map.get(CustomerEnum.nip.name()));
        }
        
        newCustomer.setCreationDate(new Date());
        newCustomer.setModificationDate(new Date());
        
        customerDao.save(newCustomer);
        sendTextForUser(SUCCESS_SAVE);
        
    }
    
    public Customer getCustomerById(int id){
       return customerDao.findById(id);
    }
    
    public void updateCustomer(Customer customer){
        customerDao.update(customer);
    }
    
    @Override
    public void sendTextForUser(String message){
        CustomerScreen sc = new CustomerScreen();
        sc.showDialogForUser(SUCCESS_SAVE);
    }
    
    public void deleteCustomer(int id){
        customerDao.delete( customerDao.findById(id) );
    }
    
    public HashMap<String, String> getCustomerByIdAsHashMap(int id){
        HashMap<String, String> map = new HashMap<>();
       try{ 
        Customer customer = customerDao.findById(id);
        map.put(CustomerEnum.firstName.name(), customer.getFirstName());
        map.put(CustomerEnum.lastName.name(), customer.getLastName());
        map.put(CustomerEnum.street.name(), customer.getStreet());
        map.put(CustomerEnum.city.name(), customer.getCity());
       }
       catch(Exception e){
           System.out.println(e);
       }
       return map;
    }
}
